package com.vshop.timingtask;

import com.vshop.entity.alast.common.MerchantServicePlanOrderRecord;
import com.vshop.entity.alast.common.ServicePlanOrderRecord;
import com.vshop.service.task.ServicePlanTaskServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 将BBC的数据同步到新商户管理系统的数据表去
 *
 * @author lm
 * @date 2021/7/15 17:20
 */

@Configuration
@EnableScheduling
@RestController
@Slf4j
public class OrderDataSyncTask {
    @Value("${system.autoTask}")
    private boolean autoTask;
    @Autowired
    ServicePlanTaskServiceImpl taskService;

    @GetMapping(value = "syncOrderTask")
    @Async
    public void execute() {
        if (autoTask) {
            syncOrderTask();
        }
    }

    public void syncOrderTask() {
        log.info("执行OrderDataSyncTask任务开始执行！");
        List<ServicePlanOrderRecord> ordersList = taskService.queryServicePlanOrderRecordList();
        if (ordersList == null || ordersList.isEmpty()) {
            log.info("执行OrderDataSyncTask任务无可同步的订单直接结束！");
            return;
        }

        for (ServicePlanOrderRecord orders : ordersList) {
            try {
                MerchantServicePlanOrderRecord or = new MerchantServicePlanOrderRecord();
                or.setPayId(orders.getPayId());
                or.setPayRst(orders.getPayRst());
                or.setPayType(orders.getPayType());
                or.setPayAccount(orders.getPayAccount());
                or.setPayMode(orders.getPayMode());
                if (orders.getPayStatus().equals("1")) {
                    or.setTradingStatus("2");
                } else if (orders.getPayStatus().equals("2")) {
                    or.setTradingStatus("3");
                }
                if (orders.getPayStatus().equals("2")) {
                    or.setRefundStatus("2");
                }
                or.setTransactionId(orders.getTransactionId());
                or.setRequestOrderId(orders.getRequestOrderId());
                or.setDeviceCode(orders.getDeviceCode());
                String sort = taskService.queryDeviceSort(orders.getDeviceCode());
                if (StringUtils.isNotBlank(sort)) {
                    or.setDeviceSort(sort);
                } else {
                    or.setDeviceSort("1");
                }
                or.setOrderNo(orders.getOrderNo());
                or.setOrderId(orders.getOrderId());
                or.setPartnerCode(orders.getPartnerCode());
                or.setPartnerName(orders.getPartnerName());
                or.setServicePlanShowName(orders.getServicePlanShowName());
                or.setServicePlanCode(orders.getServicePlanCode());
                or.setPackageName(orders.getPackageName());
                or.setPackageCode(orders.getPackageCode());
                or.setPackageType(orders.getPackageType());
                or.setOrderPeriod(orders.getOrderPeriod());
                or.setPayAmount(orders.getPayAmount());
                or.setPayCurrency(orders.getPayCurrency());
                or.setPayUnit(orders.getPayUnit());
                or.setSettlePrice(orders.getSettlePrice());
                or.setPayStatus(orders.getPayStatus());
                or.setIsPartnerNotice(orders.getIsPartnerNotice());
                or.setIsRequest(orders.getIsRequest());
                or.setIsRenew(orders.getIsRenew());
                or.setIsOpen(orders.getIsOpen());
                or.setTerminal(orders.getTerminal());
                or.setMark(orders.getMark());
                or.setCreateDate(orders.getCreateDate());
                or.setPayResultDate(orders.getPayResultDate());
                or.setOrderResultDate(orders.getOrderResultDate());
                or.setRefundDate(null);
                or.setUpdateDate(orders.getUpdateDate());
                or.setPackageStatus(orders.getPackageStatus());
                or.setOrderTime(orders.getOrderTime());
                or.setOrderStartDate(orders.getOrderStartDate());
                or.setOrderExpireDate(orders.getOrderExpireDate());
                taskService.syncOrderData(or);
            } catch (Exception e) {
                log.info("执行OrderDataSyncTask任务捕获到异常：" + e);
                continue;
            }
        }
        log.info("执行OrderDataSyncTask任务结束：同步了：" + ordersList.size() + "条");
    }

}
